package cn.spark.study.sql

import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.sql.SQLContext
import org.apache.spark.sql.Row
import org.apache.spark.sql.types.StructType
import org.apache.spark.sql.types.StructField
import org.apache.spark.sql.types.IntegerType
import org.apache.spark.sql.types.StringType


object RDD2DataFrameProgrammaticlly {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
      .setMaster("local")
      .setAppName("RDD2DataFrameProgrammaticlly");
    
    val sc = new SparkContext(conf)
    val sqlC = new SQLContext(sc)
    
    //构造对象RDD  创建 RDD<Row>
    val studentRDD = sc.textFile("C://Users//e20160504//Desktop//spark//students.json", 1).map { _.split(",") }.map { arr => 
      Row(arr(0).toInt,arr(1).toString(),arr(2).toInt)
    }
    
    //构造元数据
    val structField = StructType(Array(StructField("id",IntegerType,true),StructField("name",StringType,true),StructField("age",IntegerType,true)))
    
    //将RDD 转换成  DataFrame
    val studnetDF = sqlC.createDataFrame(studentRDD, structField)
    studnetDF.registerTempTable("student")
    val teenegerDF = sqlC.sql("select * from student where age <= 19")
    
    teenegerDF.rdd.collect().foreach { row => println(row.get(0) + ":" + row.get(1) + ":" + row.get(2)) }
    
  }
}




















